package com.tcdp.controllers.view;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.persistence.NoResultException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.tcdp.daos.GalleryDao;
import com.tcdp.daos.GalleryImageDao;
import com.tcdp.delegates.GalleryDelegate;
import com.tcdp.delegates.GalleryImageDelegate;

@Controller
@EnableWebMvc
@RequestMapping("/gallery")
public class ViewGalleryController extends AbstractViewController {

	@RequestMapping(value = "/{gallery_url}", method = RequestMethod.GET)
	public ModelAndView viewByUrl(
			@PathVariable("gallery_url") String gallery_url,HttpServletResponse response) throws IOException {

		ModelAndView mv = new ModelAndView("Gallery");

		GalleryDao galleryDao = new GalleryDao();
		GalleryDelegate galleryDel= null;
		try{
		galleryDel = new GalleryDelegate(
				galleryDao.findByUrl(gallery_url));
		}catch(NoResultException e){
			throwError(response, HttpServletResponse.SC_NOT_FOUND);
			return null;
		}
		mv.addObject("gallery", galleryDel);

		return mv;
	}

	@RequestMapping(value = "popup/{gallery_url}/{img_url}.{img_type}", method = RequestMethod.POST)
	public ModelAndView imagePopup(@PathVariable("img_url") String img_url,
			@PathVariable("gallery_url") String gallery_url,
			@PathVariable("img_type") String img_type) {

		ModelAndView mv = new ModelAndView("ImagePopup");
		
		GalleryImageDao galleryImageDao = new GalleryImageDao();
		GalleryImageDelegate galleryImageDel = new GalleryImageDelegate(galleryImageDao.findByUrl(img_url + "." + img_type));
		
		mv.addObject("galleryImage", galleryImageDel);
		mv.addObject("galleryUrl", gallery_url);
		
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = "/{gallery_url}/{img_url}.{img_type}", method = RequestMethod.GET, produces = {
			MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_GIF_VALUE,
			MediaType.IMAGE_PNG_VALUE })
	public byte[] loadPhoto(@PathVariable("img_url") String img_url,
			@PathVariable("gallery_url") String gallery_url,
			@PathVariable("img_type") String img_type,
			HttpServletRequest request) throws IOException {
		
		GalleryImageDao galleryImageDao = new GalleryImageDao();
		String id = galleryImageDao.findIdByUrl(img_url + "." + img_type);

		URL url = request.getServletContext().getResource(
				"/WEB-INF/multimedia/photos/" + gallery_url + "/" + id + "."
						+ img_type);
		int length = url.openConnection().getContentLength();
		InputStream is = url.openStream();
		byte[] data = new byte[length];
		is.read(data);

		return (data);
	}

}
